﻿using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace BigBallz.Core.Web.MVC.Filters
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Web;

    [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes",
        Justification = "This attribute is AllowMultiple = true and users might want to override behavior.")]
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class ExceptionHandlerAttribute : FilterAttribute, IExceptionFilter
    {

        private const string _defaultView = "Error";

        private Type _exceptionType = typeof(Exception);
        private string _master;
        private string _view;

        public string RedirectToAction { get; set; }
        public string RedirectToController { get; set; }

        public Type ExceptionType
        {
            get
            {
                return _exceptionType;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (!typeof(Exception).IsAssignableFrom(value))
                {
                    throw new ArgumentException(String.Format(CultureInfo.CurrentUICulture,
                        "Type {0} is not an exception", value.FullName));
                }

                _exceptionType = value;
            }
        }

        public string Master
        {
            get
            {
                return _master ?? String.Empty;
            }
            set
            {
                _master = value;
            }
        }

        public string View
        {
            get
            {
                return (!String.IsNullOrEmpty(_view)) ? _view : _defaultView;
            }
            set
            {
                _view = value;
            }
        }

        public virtual void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            // If custom errors are disabled, we need to let the normal ASP.NET exception handler
            // execute so that the user can see useful debugging information.
            if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
            {
                return;
            }

            Exception exception = filterContext.Exception;

            // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
            // ignore it.
            if (new HttpException(null, exception).GetHttpCode() != 500)
            {
                return;
            }

            if (!ExceptionType.IsInstanceOfType(exception))
            {
                return;
            }

            filterContext.ExceptionHandled = true;

            if (!String.IsNullOrEmpty(RedirectToAction))
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(
                                                                     new
                                                                         {
                                                                             action = RedirectToAction,
                                                                             controller = String.IsNullOrEmpty(RedirectToController) ? (string)filterContext.RouteData.Values["controller"] : RedirectToController,
                                                                             aspxerrorpath = filterContext.RequestContext.HttpContext.Request.Path
                                                                         }));
            else
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonResult
                                               {
                                                   Data = RenderAjaxErrorMessage(filterContext.Exception)
                                               };
                }
                else
                {
                    string controllerName = (string)filterContext.RouteData.Values["controller"];
                    string actionName = (string)filterContext.RouteData.Values["action"];
                    HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);

                    filterContext.Result = new ViewResult
                                               {
                                                   ViewName = View,
                                                   MasterName = Master,
                                                   ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
                                                   TempData = filterContext.Controller.TempData
                                               };
                }

                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.StatusCode = 500;

                // Certain versions of IIS will sometimes use their own error page when
                // they detect a server error. Setting this property indicates that we
                // want it to try to render ASP.NET MVC's error page instead.
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            }
        }

        private static string RenderAjaxErrorMessage(Exception exception)
        {
            var message = new StringBuilder();
            var error = exception.InnerException;
            while (error != null)
            {
                message.AppendLine(error.Message);
                error = error.InnerException;
            }

            return message.ToString();
        }
    }
}